

//对象直接赋值给对象
#include <iostream>
#include <cstring>
using namespace std;

class point
{

    int x;
    char *str;

public:
    point(int m_x, const char *m_str) : x(m_x) // point(int m_x, char *m_str) : x(m_x)
    {
        str = new char[strlen(m_str) + 1];
        strcpy(str, m_str);
    };
    point &operator=(const point &m_point) //可以允许被赋值，所以返回值不是const.   其次x和y的值会改变，所以函数不能是常函数

    {                         //判断自赋值的情况
        if (this == &m_point) //  不能if(*this==m_point)因为同地址必然同值，但同值不一定同地址
            return *this;
        else
            x = m_point.x;

        str = new char[strlen(m_point.str) + 1];
        strcpy(str, m_point.str);
        return *this;
        delete[] str;
    }
    void display()
    {
        cout << x << str << endl;
    }
    ~point()
    {
        delete[] str;
        cout << "deconstruct" << endl;
    }
};

int main()
{
    point p1(2, "abcd");
    point p2(1, "efgh");
    p2 = p1;

    p2.display();
    return 0;
}

//子类运算符重载函数也能调用父类的运算符重载函数
#include <iostream>
using namespace std;

class father
{
public:
    int a;

public:
    father &operator=(const father &m_f)
    {
        a = m_f.a;
        return *this;
    }
};
class son : public father
{
public:
    int b;

public:
    son &operator=(const son &m_s)
    {
        b = m_s.b;
        father::operator=(m_s); //调用父类的赋值运算函数
        return *this;
    }
};
int main()
{
    son s1;
    s1.a = 23;
    s1.b = 32;
    cout << s1.a << s1.b << endl;
};